#include <list>
#include <vector>
#include <map>
#include <string>
#include <stdio.h>

int main()
{
    printf("1------\n");
    {
        std::list<int> list({1,3,5,6,7,5,3,2,1,6,6});
        for (auto it = list.begin(); it != list.end(); it++) {
            if (*it <= 3) {
                list.erase(it--);
            }
        }

        for (auto i : list) {
            printf("%d\n", i);
        }
    }

    printf("2------\n");
    {
        std::list<int> list({1,3,5,6,7,5,3,2,1,6,6});
        for (auto it = list.begin(); it != list.end(); it++) {
            if (*it > 3) {
                list.erase(it--);
            }
        }

        for (auto i : list) {
            printf("%d\n", i);
        }
    }

    printf("3------\n");
    {
        std::list<int> list({1,3,5,6,7,5,3,2,1,6,6});
        auto it = list.begin();
        printf("list value: %d\n", *it);
        it--;
        printf("list value: %d\n", *it);
        it++;
        printf("list value: %d\n", *it);

        printf("list addr[0]: %p\n", &*it);
        it++;
        printf("list addr[1]: %p\n", &*it);
        it--;
        it--;
        printf("list addr[-1]: %p\n", &*it);
        it++;
        printf("list addr[0]: %p\n", &*it);
    }

    printf("4------\n");
    {
        std::vector<int> vector({1,3,5,6,7,5,3,2,1,6,6});
        auto it = vector.begin();
        printf("vector value: %d\n", *it);
        it--;
        printf("vector value: %d\n", *it);
        it++;
        printf("vector value: %d\n", *it);

        printf("vector addr[0]: %p\n", &*it);
        it++;
        printf("vector addr[1]: %p\n", &*it);
        it--;
        it--;
        printf("vector addr[-1]: %p\n", &*it);
        it++;
        printf("vector addr[0]: %p\n", &*it);
    }

    printf("5------\n");
    {
        std::list<int> list;
        for (auto it = list.begin(); it != list.end(); it++) {
            if (*it > 3) {
                list.erase(it--);
            }
        }

        for (auto i : list) {
            printf("%d\n", i);
        }
    }

    printf("6------\n");
    {
        std::map<int, std::string> map({
            {1, "m13"},
            {2, "m23"},
            {3, "m33"},
            {4, "m43"},
            {5, "m53"},
            {6, "m63"},
        });
        for (auto it = map.begin(); it != map.end();) {
            if (it->first > 3) {
                map.erase(it++);
            } else {
                it++;
            }
        }

        for (const auto &[k, v] : map) {
            printf("%d, %s\n", k, v.c_str());
        }
    }

    printf("7------\n");
    {
        std::map<int, std::string> map({
            {1, "m13"},
            {2, "m23"},
            {3, "m33"},
            {4, "m43"},
            {5, "m53"},
            {6, "m63"},
        });
        for (auto it = map.begin(); it != map.end();) {
            if (it->first <= 3) {
                map.erase(it++);
            } else {
                it++;
            }
        }

        for (const auto &[k, v] : map) {
            printf("%d, %s\n", k, v.c_str());
        }
    }

    printf("8------\n");
    {
        std::map<int, std::string> map({
            {1, "m13"},
            {2, "m23"},
            {3, "m33"},
            {4, "m43"},
            {5, "m53"},
            {6, "m63"},
        });
        auto it = map.begin();
        printf("vector value: %d\n", it->first);
        it--;
        printf("vector value: %d\n", it->first);
        it++;
        printf("vector value: %d\n", it->first);

        printf("vector addr[0]: %p\n", &*it);
        it++;
        printf("vector addr[1]: %p\n", &*it);
        it--;
        it--;
        printf("vector addr[-1]: %p\n", &*it);
        it++;
        printf("vector addr[0]: %p\n", &*it);
    }

    printf("9------\n");
    {
        std::list<int> list({1,3,5,6,7,5,3,2,1,6,6});
        for (auto it = list.begin(); it != list.end();) {
            if (*it <= 3) {
                list.erase(it++);
            } else {
                it++;
            }
        }

        for (auto i : list) {
            printf("%d\n", i);
        }
    }

    printf("10------\n");
    {
        std::list<int> list({1,3,5,6,7,5,3,2,1,6,6});
        for (auto it = list.begin(); it != list.end();) {
            if (*it > 3) {
                list.erase(it++);
            } else {
                it++;
            }
        }

        for (auto i : list) {
            printf("%d\n", i);
        }
    }

    {
        std::list<int> list({1,3,5,6,7,5,3,2,1,6,6});
        auto it = list.begin();
        auto itnext = it;
        itnext++;
        printf("%p\n", &*it);
        printf("%p\n", &*itnext);

        list.erase(it++);
        printf("%p\n", &*it);
        printf("%p\n", &*itnext);
    }
    return 0;
}
